using System;
using System.Collections.Generic;
using System.Linq;
using Df.Common.Forms.JsonFormatter;
using Df.Framework.Serialization;
using Newtonsoft.Json;

namespace Df.Common.Forms
{
    [JsonConverter(typeof(ElementConverter))]
    public class DfDropDown : DfBaseElement, IInputElement
    {
        private string _stringElements;

        public List<DfElementItem> Items { get; set; }

        [JsonIgnore]
        public string JsonItems
        {
            get
            {

                if (Items != null && Items.Any())
                {
                    JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
                    _stringElements = JsonHelper.JsonSerializer(Items, settings);
                }
                return _stringElements;

            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _stringElements = value;
                    JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
                    Items = JsonHelper.JsonDeserialize<List<DfElementItem>>(_stringElements, settings);
                }
            }
        }


        public string Value
        {
            get;
            set;
        }

        public override object GetValue()
        {
            return Items.Single(i => i.Value == Value).Text;
        }


        public bool IsRequired
        {
            get;
            set;
        }   

    }
}